<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>difference</title>
<link rel="stylesheet" href="../../../../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../../../index.html" title="Chapter 1. Geometry">
<link rel="up" href="../difference.html" title="difference">
<link rel="prev" href="difference_4_with_strategy.html" title="difference (with strategy)">
<link rel="next" href="../discrete_frechet_distance.html" title="discrete_frechet_distance">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="difference_4_with_strategy.html"><img src="../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../difference.html"><img src="../../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../../index.html"><img src="../../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../discrete_frechet_distance.html"><img src="../../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h5 class="title">
<a name="geometry.reference.algorithms.difference.difference_3"></a><a class="link" href="difference_3.html" title="difference">difference</a>
</h5></div></div></div>
<p>
            <a class="indexterm" name="idm45246443481856"></a>
          </p>
<h6>
<a name="geometry.reference.algorithms.difference.difference_3.h0"></a>
            <span class="phrase"><a name="geometry.reference.algorithms.difference.difference_3.description"></a></span><a class="link" href="difference_3.html#geometry.reference.algorithms.difference.difference_3.description">Description</a>
          </h6>
<p>
            Calculate the difference of two geometries
          </p>
<p>
            The free function difference calculates the spatial set theoretic difference
            of two geometries.
          </p>
<h6>
<a name="geometry.reference.algorithms.difference.difference_3.h1"></a>
            <span class="phrase"><a name="geometry.reference.algorithms.difference.difference_3.synopsis"></a></span><a class="link" href="difference_3.html#geometry.reference.algorithms.difference.difference_3.synopsis">Synopsis</a>
          </h6>
<p>
</p>
<pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">Geometry1</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Geometry2</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Collection</span><span class="special">&gt;</span>
<span class="keyword">void</span> <span class="identifier">difference</span><span class="special">(</span><span class="identifier">Geometry1</span> <span class="keyword">const</span> <span class="special">&amp;</span> <span class="identifier">geometry1</span><span class="special">,</span> <span class="identifier">Geometry2</span> <span class="keyword">const</span> <span class="special">&amp;</span> <span class="identifier">geometry2</span><span class="special">,</span> <span class="identifier">Collection</span> <span class="special">&amp;</span> <span class="identifier">output_collection</span><span class="special">)</span></pre>
<p>
          </p>
<h6>
<a name="geometry.reference.algorithms.difference.difference_3.h2"></a>
            <span class="phrase"><a name="geometry.reference.algorithms.difference.difference_3.parameters"></a></span><a class="link" href="difference_3.html#geometry.reference.algorithms.difference.difference_3.parameters">Parameters</a>
          </h6>
<div class="informaltable"><table class="table">
<colgroup>
<col>
<col>
<col>
<col>
</colgroup>
<thead><tr>
<th>
                    <p>
                      Type
                    </p>
                  </th>
<th>
                    <p>
                      Concept
                    </p>
                  </th>
<th>
                    <p>
                      Name
                    </p>
                  </th>
<th>
                    <p>
                      Description
                    </p>
                  </th>
</tr></thead>
<tbody>
<tr>
<td>
                    <p>
                      Geometry1 const &amp;
                    </p>
                  </td>
<td>
                    <p>
                      Any type fulfilling a Geometry Concept
                    </p>
                  </td>
<td>
                    <p>
                      geometry1
                    </p>
                  </td>
<td>
                    <p>
                      A model of the specified concept
                    </p>
                  </td>
</tr>
<tr>
<td>
                    <p>
                      Geometry2 const &amp;
                    </p>
                  </td>
<td>
                    <p>
                      Any type fulfilling a Geometry Concept
                    </p>
                  </td>
<td>
                    <p>
                      geometry2
                    </p>
                  </td>
<td>
                    <p>
                      A model of the specified concept
                    </p>
                  </td>
</tr>
<tr>
<td>
                    <p>
                      Collection &amp;
                    </p>
                  </td>
<td>
                    <p>
                      output collection, either a multi-geometry, or a std::vector&lt;Geometry&gt;
                      / std::deque&lt;Geometry&gt; etc
                    </p>
                  </td>
<td>
                    <p>
                      output_collection
                    </p>
                  </td>
<td>
                    <p>
                      the output collection
                    </p>
                  </td>
</tr>
</tbody>
</table></div>
<h6>
<a name="geometry.reference.algorithms.difference.difference_3.h3"></a>
            <span class="phrase"><a name="geometry.reference.algorithms.difference.difference_3.header"></a></span><a class="link" href="difference_3.html#geometry.reference.algorithms.difference.difference_3.header">Header</a>
          </h6>
<p>
            Either
          </p>
<p>
            <code class="computeroutput"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">geometry</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span></code>
          </p>
<p>
            Or
          </p>
<p>
            <code class="computeroutput"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">geometry</span><span class="special">/</span><span class="identifier">algorithms</span><span class="special">/</span><span class="identifier">difference</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span></code>
          </p>
<h6>
<a name="geometry.reference.algorithms.difference.difference_3.h4"></a>
            <span class="phrase"><a name="geometry.reference.algorithms.difference.difference_3.conformance"></a></span><a class="link" href="difference_3.html#geometry.reference.algorithms.difference.difference_3.conformance">Conformance</a>
          </h6>
<p>
            The function difference implements function Difference from the <a href="http://www.opengeospatial.org/standards/sfa" target="_top">OGC Simple Feature
            Specification</a>.
          </p>
<h6>
<a name="geometry.reference.algorithms.difference.difference_3.h5"></a>
            <span class="phrase"><a name="geometry.reference.algorithms.difference.difference_3.behavior"></a></span><a class="link" href="difference_3.html#geometry.reference.algorithms.difference.difference_3.behavior">Behavior</a>
          </h6>
<div class="informaltable"><table class="table">
<colgroup>
<col>
<col>
</colgroup>
<thead><tr>
<th>
                    <p>
                      Case
                    </p>
                  </th>
<th>
                    <p>
                      Behavior
                    </p>
                  </th>
</tr></thead>
<tbody>
<tr>
<td>
                    <p>
                      areal (e.g. polygon)
                    </p>
                  </td>
<td>
                    <p>
                      All combinations of: box, ring, polygon, multi_polygon
                    </p>
                  </td>
</tr>
<tr>
<td>
                    <p>
                      linear (e.g. linestring) / areal (e.g. polygon)
                    </p>
                  </td>
<td>
                    <p>
                      A combinations of a (multi) linestring with a (multi) polygon
                      results in a collection of linestrings
                    </p>
                  </td>
</tr>
<tr>
<td>
                    <p>
                      linear (e.g. linestring)
                    </p>
                  </td>
<td>
                    <p>
                      All combinations of: linestring, multi_linestring; results
                      in a collection of linestrings
                    </p>
                  </td>
</tr>
<tr>
<td>
                    <p>
                      pointlike (e.g. point)
                    </p>
                  </td>
<td>
                    <p>
                      All combinations of: point, multi_point; results in a collection
                      of points
                    </p>
                  </td>
</tr>
<tr>
<td>
                    <p>
                      Other geometries
                    </p>
                  </td>
<td>
                    <p>
                      Not yet supported in this version
                    </p>
                  </td>
</tr>
<tr>
<td>
                    <p>
                      Spherical
                    </p>
                  </td>
<td>
                    <p>
                      Not yet supported in this version
                    </p>
                  </td>
</tr>
<tr>
<td>
                    <p>
                      Three dimensional
                    </p>
                  </td>
<td>
                    <p>
                      Not yet supported in this version
                    </p>
                  </td>
</tr>
</tbody>
</table></div>
<div class="note"><table border="0" summary="Note">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../../../../doc/src/images/note.png"></td>
<th align="left">Note</th>
</tr>
<tr><td align="left" valign="top"><p>
              Check the <a class="link" href="../../concepts/concept_polygon.html" title="Polygon Concept">Polygon
              Concept</a> for the rules that polygon input for this algorithm
              should fulfill
            </p></td></tr>
</table></div>
<h6>
<a name="geometry.reference.algorithms.difference.difference_3.h6"></a>
            <span class="phrase"><a name="geometry.reference.algorithms.difference.difference_3.example"></a></span><a class="link" href="difference_3.html#geometry.reference.algorithms.difference.difference_3.example">Example</a>
          </h6>
<p>
            Shows how to subtract one polygon from another polygon
          </p>
<p>
</p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">iostream</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">list</span><span class="special">&gt;</span>

<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">geometry</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">geometry</span><span class="special">/</span><span class="identifier">geometries</span><span class="special">/</span><span class="identifier">point_xy</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">geometry</span><span class="special">/</span><span class="identifier">geometries</span><span class="special">/</span><span class="identifier">polygon</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>

<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">foreach</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>


<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span>
<span class="special">{</span>
    <span class="keyword">typedef</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">geometry</span><span class="special">::</span><span class="identifier">model</span><span class="special">::</span><span class="identifier">polygon</span><span class="special">&lt;</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">geometry</span><span class="special">::</span><span class="identifier">model</span><span class="special">::</span><span class="identifier">d2</span><span class="special">::</span><span class="identifier">point_xy</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;</span> <span class="special">&gt;</span> <span class="identifier">polygon</span><span class="special">;</span>

    <span class="identifier">polygon</span> <span class="identifier">green</span><span class="special">,</span> <span class="identifier">blue</span><span class="special">;</span>

    <span class="identifier">boost</span><span class="special">::</span><span class="identifier">geometry</span><span class="special">::</span><span class="identifier">read_wkt</span><span class="special">(</span>
        <span class="string">"POLYGON((2 1.3,2.4 1.7,2.8 1.8,3.4 1.2,3.7 1.6,3.4 2,4.1 3,5.3 2.6,5.4 1.2,4.9 0.8,2.9 0.7,2 1.3)"</span>
            <span class="string">"(4.0 2.0, 4.2 1.4, 4.8 1.9, 4.4 2.2, 4.0 2.0))"</span><span class="special">,</span> <span class="identifier">green</span><span class="special">);</span>

    <span class="identifier">boost</span><span class="special">::</span><span class="identifier">geometry</span><span class="special">::</span><span class="identifier">read_wkt</span><span class="special">(</span>
        <span class="string">"POLYGON((4.0 -0.5 , 3.5 1.0 , 2.0 1.5 , 3.5 2.0 , 4.0 3.5 , 4.5 2.0 , 6.0 1.5 , 4.5 1.0 , 4.0 -0.5))"</span><span class="special">,</span> <span class="identifier">blue</span><span class="special">);</span>

    <span class="identifier">std</span><span class="special">::</span><span class="identifier">list</span><span class="special">&lt;</span><span class="identifier">polygon</span><span class="special">&gt;</span> <span class="identifier">output</span><span class="special">;</span>
    <span class="identifier">boost</span><span class="special">::</span><span class="identifier">geometry</span><span class="special">::</span><span class="identifier">difference</span><span class="special">(</span><span class="identifier">green</span><span class="special">,</span> <span class="identifier">blue</span><span class="special">,</span> <span class="identifier">output</span><span class="special">);</span>

    <span class="keyword">int</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span>
    <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"green - blue:"</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
    <span class="identifier">BOOST_FOREACH</span><span class="special">(</span><span class="identifier">polygon</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">p</span><span class="special">,</span> <span class="identifier">output</span><span class="special">)</span>
    <span class="special">{</span>
        <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">i</span><span class="special">++</span> <span class="special">&lt;&lt;</span> <span class="string">": "</span> <span class="special">&lt;&lt;</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">geometry</span><span class="special">::</span><span class="identifier">area</span><span class="special">(</span><span class="identifier">p</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
    <span class="special">}</span>


    <span class="identifier">output</span><span class="special">.</span><span class="identifier">clear</span><span class="special">();</span>
    <span class="identifier">boost</span><span class="special">::</span><span class="identifier">geometry</span><span class="special">::</span><span class="identifier">difference</span><span class="special">(</span><span class="identifier">blue</span><span class="special">,</span> <span class="identifier">green</span><span class="special">,</span> <span class="identifier">output</span><span class="special">);</span>

    <span class="identifier">i</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span>
    <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"blue - green:"</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
    <span class="identifier">BOOST_FOREACH</span><span class="special">(</span><span class="identifier">polygon</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">p</span><span class="special">,</span> <span class="identifier">output</span><span class="special">)</span>
    <span class="special">{</span>
        <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">i</span><span class="special">++</span> <span class="special">&lt;&lt;</span> <span class="string">": "</span> <span class="special">&lt;&lt;</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">geometry</span><span class="special">::</span><span class="identifier">area</span><span class="special">(</span><span class="identifier">p</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
    <span class="special">}</span>


    <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
          </p>
<p>
            Output:
          </p>
<pre class="programlisting">green - blue:
0: 0.02375
1: 0.542951
2: 0.0149697
3: 0.226855
4: 0.839424

<img src="../../../../img/algorithms/difference_a.png" alt="difference_a">

blue - green:
0: 0.525154
1: 0.015
2: 0.181136
3: 0.128798
4: 0.340083
5: 0.307778

<img src="../../../../img/algorithms/difference_b.png" alt="difference_b">
</pre>
<h6>
<a name="geometry.reference.algorithms.difference.difference_3.h7"></a>
            <span class="phrase"><a name="geometry.reference.algorithms.difference.difference_3.see_also"></a></span><a class="link" href="difference_3.html#geometry.reference.algorithms.difference.difference_3.see_also">See
            also</a>
          </h6>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
                <a class="link" href="../sym_difference.html" title="sym_difference">sym_difference
                (symmetric difference)</a>
              </li>
<li class="listitem">
                <a class="link" href="../intersection.html" title="intersection">intersection</a>
              </li>
<li class="listitem">
                <a class="link" href="../union_.html" title="union_">union</a>
              </li>
</ul></div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2009-2021 Barend Gehrels,
      Bruno Lalande, Mateusz Loskot, Adam Wulkiewicz, Oracle and/or its affiliates<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="difference_4_with_strategy.html"><img src="../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../difference.html"><img src="../../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../../index.html"><img src="../../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../discrete_frechet_distance.html"><img src="../../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
